package com.webull.information.center.carwler.common.util.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.TimeZone;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.framework.util.UtilDate;
import com.webull.information.center.carwler.common.model.StockTitle;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;

/**
 * 新浪个股新闻资讯列表解析工具
 * 
 * @author shimingjun
 * @date 2016年8月26日 下午3:51:37
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class MctrolNewsListUitl {
	private @Autowired
    HttpJsoupClient htmlPrase;
	protected final Logger logger = LogManager.getLogger(getClass());
	private String searchTit = "http://www.moneycontrol.com/stocks/cptmarket/compsearchnew.php?search_data=&cid=&mbsearch_str=&topsearch_type=1&search_str={0}";

	/**
	 * 获取新浪个股资讯首页对象并解析新闻条目列表
	 * 
	 * @param url
	 * @return
	 * @since 0.1.0
	 */
	public List<MctrolItem> parseNewsList(StockTitle stockTitle) {
		String url = StringUtils.replace(searchTit, "{0}", stockTitle.getSymbol(), 1);
		Document doc = htmlPrase.parseDocumentFromUrl(StringUtils.replace(searchTit, "{0}", url, 1), true);
		if (doc == null) {
			logger.warn("MctrolNewsListUitl parseNewsList parse stockTitle:{} ,is null!!", stockTitle);
			return null;
		}
		String newsList = Optional.ofNullable(doc.select("dl#slider").first())//
				.map(v -> v.select("dt>a:containsOwn(NEWS)").first())//
				.map(v -> v.absUrl("href")).orElse(null);

		if (StringUtils.isBlank(newsList)) {
			logger.warn("MctrolNewsListUitl parseNewsList parse newsList:{} ,str is null!!", newsList);
			return null;
		}
		Document doc2 = htmlPrase.parseDocumentFromUrl(newsList, true);
		if (doc2 == null) {
			logger.warn("MctrolNewsListUitl parseNewsList parse newsList:{} ,doc is null!!", newsList);
			return null;
		}
		return parseNewsListIn(doc2);
	}

	private static List<MctrolItem> parseNewsListIn(Document doc) {
		List<MctrolItem> result = new ArrayList<MctrolItem>();

		// 美股新闻页面
		Optional.ofNullable(doc.select("div.mntYrs").first())//
				.map(v -> v.nextElementSibling())//
				.map(v -> v.select("div>div.FL").first())//
				.map(v -> v.select("div>div:has(div)"))//
				.ifPresent(eles -> {
					for (int i = 0; i < eles.size(); i++) {
						Element ele = eles.get(i);
						MctrolItem item = new MctrolItem();
						String title = Optional.ofNullable(ele.select("div>div.FL>a>strong").first())
								.map(str -> str.ownText()).orElse(null);
						item.setTitle(title);
						String link = Optional.ofNullable(ele.select("div>div.FL>a:has(strong)").first())
								.map(str -> str.absUrl("href")).orElse(null);
						item.setLink(link);
						String summy = Optional.ofNullable(ele.select("div>div.FL>p[class=PT3]").first())
								.map(str -> str.ownText())//
								.filter(str -> !StringUtils.startsWithIgnoreCase(str, "NULL"))//
								.orElse(null);
						item.setSummy(summy);
						String img = Optional.ofNullable(ele.select("div.FL>a>img").first())
								.map(str -> str.absUrl("data-original")).orElse(null);
						item.setImg(img);
						Optional.ofNullable(ele.select("div.FL>p.PT3:has(span)").first()).ifPresent(ele0 -> {

							item.setSoure(ele0.select("span").first().ownText());
							// 12.16 pm | 16 Jan 2017 | Source:
							// 8.20 am | 28 Mar 2017 | Source: PTI

							String dateStr = ele0.ownText() != null ? ele0.ownText() : "";
							dateStr = dateStr.replaceAll("\\u00A0", "");
							dateStr = StringUtils.substringBeforeLast(dateStr, "|");
							dateStr = StringUtils.remove(dateStr, "|");
							dateStr = StringUtils.replacePattern(dateStr, "\\s{2,}", " ");
							dateStr = StringUtils.stripToNull(dateStr);
							try {
								Date newTime = UtilDate.parse(dateStr, Locale.US, TimeZone.getTimeZone("Asia/Kolkata"),
										"hh.mm aa d MMM yyyy");
								item.setNewsTime(newTime);
							} catch (Exception e) {
							}
						});

						if (StringUtils.isNotBlank(item.getLink())//
								&& item.getNewsTime() != null //
								&& StringUtils.isNotBlank(item.getTitle())) {
							result.add(item);
						}

					}
				});

		return result;
	}

	public static void main(String[] args) throws IOException {

		// String url2 =
		// "http://www.moneycontrol.com/stocks/cptmarket/compsearchnew.php?search_data=&cid=&mbsearch_str=&topsearch_type=1&search_str=522005";

		String url3 = "http://www.moneycontrol.com/company-article/sangamadvisors/news/SA04#SA04";
		Connection connection = Jsoup.connect(url3).userAgent(
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
				// .header("x-client-data",
				// "CIq2yQEIpbbJAQjEtskBCP2VygEIwpjKAQjwnMoB")
				.header("x-client-data", RandomStringUtils.randomAlphanumeric(40));
		connection.proxy("127.0.0.1", 1080);

		org.jsoup.nodes.Document doc = connection.timeout(10000).get();
		// List<SinaItem> items = new MctrolNewsListUitl().parseNewsList(doc);
		// List<SinaItem> items = new MctrolNewsListUitl().parseNewsListHK(doc);
		parseNewsListIn(doc);
		System.out.println(11);
	}

	public static class MctrolItem {
		private Date newsTime;
		private String newsTimeStr;
		private String title;
		private String summy;
		private String link;
		private String img;
		private String soure;

		public String getSoure() {
			return soure;
		}

		public void setSoure(String soure) {
			this.soure = soure;
		}

		public String getImg() {
			return img;
		}

		public void setImg(String img) {
			this.img = img;
		}

		public String getSummy() {
			return summy;
		}

		public void setSummy(String summy) {
			this.summy = summy;
		}

		public Date getNewsTime() {
			return newsTime;
		}

		public void setNewsTime(Date newsTime) {
			this.newsTime = newsTime;
		}

		public String getNewsTimeStr() {
			return newsTimeStr;
		}

		public void setNewsTimeStr(String newsTimeStr) {
			this.newsTimeStr = newsTimeStr;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getLink() {
			return link;
		}

		public void setLink(String link) {
			this.link = link;
		}

		@Override
		public String toString() {
			return "MctrolItem [newsTime=" + newsTime + ", newsTimeStr=" + newsTimeStr + ", title=" + title + ", summy="
					+ summy + ", link=" + link + ", img=" + img + ", soure=" + soure + "]";
		}

	}

}
